<template>
  <view class="finance-page">

    <scroll-view class="main-content" scroll-y="true">


      <!-- Recent Transactions -->
      <view class="transactions-section">
        <view class="section-header">
          <text class="section-title">最近交易</text>
          <text class="view-all" @tap="viewAllTransactions">查看全部</text>
        </view>
        
        <view class="transaction-list">
          <!-- Income Transaction -->
          <view class="transaction-item income">
            <view class="transaction-info">
              <text class="transaction-icon fas fa-briefcase"></text>
              <view class="transaction-details">
                <text class="transaction-title">工资收入</text>
                <text class="transaction-category">主动收入 • 今天 09:00</text>
              </view>
            </view>
            <text class="transaction-amount positive">+¥12,000</text>
          </view>

          <!-- Expense Transaction -->
          <view class="transaction-item expense">
            <view class="transaction-info">
              <text class="transaction-icon fas fa-utensils"></text>
              <view class="transaction-details">
                <text class="transaction-title">午餐</text>
                <text class="transaction-category">餐饮 • 今天 12:30</text>
              </view>
            </view>
            <text class="transaction-amount negative">-¥45</text>
          </view>

          <!-- Investment Transaction -->
          <view class="transaction-item investment">
            <view class="transaction-info">
              <text class="transaction-icon fas fa-chart-line"></text>
              <view class="transaction-details">
                <text class="transaction-title">股票分红</text>
                <text class="transaction-category">被动收入 • 昨天 15:20</text>
              </view>
            </view>
            <text class="transaction-amount positive">+¥320</text>
          </view>

          <!-- Shopping Transaction -->
          <view class="transaction-item expense">
            <view class="transaction-info">
              <text class="transaction-icon fas fa-shopping-cart"></text>
              <view class="transaction-details">
                <text class="transaction-title">超市购物</text>
                <text class="transaction-category">生活用品 • 昨天 18:45</text>
              </view>
            </view>
            <text class="transaction-amount negative">-¥156</text>
          </view>
        </view>
      </view>
      <!-- Financial Summary -->
      <view class="summary-section">
        <text class="section-title">财务概览</text>
        
        <!-- Assets & Liabilities -->
        <view class="summary-grid">
          <view class="summary-card assets">
            <view class="card-header">
              <text class="card-icon fas fa-dollar-sign"></text>
              <text class="card-title">总资产</text>
            </view>
            <text class="amount positive">¥125,800</text>
            <text class="change positive">+¥2,300</text>
          </view>
          
          <view class="summary-card liabilities">
            <view class="card-header">
              <text class="card-icon fas fa-credit-card"></text>
              <text class="card-title">总负债</text>
            </view>
            <text class="amount negative">¥45,200</text>
            <text class="change negative">+¥800</text>
          </view>
        </view>

        <!-- Income Section -->
        <view class="income-section">
          <text class="subsection-title">本月收入</text>
          <view class="income-grid">
            <view class="income-card active">
              <text class="income-icon fas fa-briefcase"></text>
              <text class="income-label">主动收入</text>
              <text class="income-amount">¥12,000</text>
            </view>
            <view class="income-card passive">
              <text class="income-icon fas fa-piggy-bank"></text>
              <text class="income-label">被动收入</text>
              <text class="income-amount">¥1,200</text>
            </view>
          </view>
        </view>

        <!-- Expenses & Cash Flow -->
        <view class="summary-grid">
          <view class="summary-card expenses">
            <view class="card-header">
              <text class="card-icon fas fa-sack-dollar"></text>
              <text class="card-title">总支出</text>
            </view>
            <text class="amount negative">¥8,500</text>
            <text class="change positive">-¥300</text>
          </view>
          
          <view class="summary-card cashflow">
            <view class="card-header">
              <text class="card-icon fas fa-chart-line"></text>
              <text class="card-title">现金流</text>
            </view>
            <text class="amount positive">¥4,700</text>
            <text class="change positive">+¥500</text>
          </view>
        </view>

        <!-- Passive Income Ratio -->
        <view class="ratio-card">
          <view class="ratio-header">
            <text class="ratio-title">被动收入占比</text>
            <text class="ratio-percentage">9.1%</text>
          </view>
          <view class="ratio-bar">
            <view class="ratio-fill" :style="{width: '9.1%'}"></view>
          </view>
          <text class="ratio-target">目标: 20%</text>
        </view>
      </view>

    </scroll-view>

    <!-- 浮动操作按钮 -->
    <uni-fab
      ref="fab"
      :pattern="pattern"
      :content="content"
      :horizontal="'right'"
      :vertical="'bottom'"
      :direction="'horizontal'"
      @trigger="trigger"
      @fabClick="fabClick"
    ></uni-fab>
  </view>
</template>

<script>
export default {
  name: 'FinancePage',
  data() {
    return {
      // uni-fab 配置
      pattern: {
        color: '#FFFFFF',
        backgroundColor: '#10B981',
        selectedColor: '#10B981',
        buttonColor: '#10B981',
        iconColor: '#FFFFFF'
      },
      content: [],
      financialData: {
        totalAssets: 125800,
        totalLiabilities: 45200,
        activeIncome: 12000,
        passiveIncome: 1200,
        totalExpenses: 8500,
        cashFlow: 4700
      },
      monthlyIncome: 5280,
      monthlyExpense: 3420,
      recentTransactions: [
        {
          id: 1,
          type: 'income',
          category: '工资收入',
          description: '2024年1月工资',
          amount: '8,000',
          date: '今天 09:30'
        },
        {
          id: 2,
          type: 'expense',
          category: '餐饮美食',
          description: '午餐 - 沙县小吃',
          amount: '35',
          date: '今天 12:15'
        },
        {
          id: 3,
          type: 'expense',
          category: '交通出行',
          description: '地铁 - 1号线',
          amount: '6',
          date: '今天 08:45'
        },
        {
          id: 4,
          type: 'income',
          category: '兼职收入',
          description: '设计项目费用',
          amount: '1,200',
          date: '昨天 16:20'
        },
        {
          id: 5,
          type: 'expense',
          category: '购物消费',
          description: '超市购物',
          amount: '128',
          date: '昨天 19:30'
        }
      ]
    }
  },
  computed: {
    passiveIncomeRatio() {
      const total = this.financialData.activeIncome + this.financialData.passiveIncome
      return ((this.financialData.passiveIncome / total) * 100).toFixed(1)
    },
    monthlyBalance() {
      return this.monthlyIncome - this.monthlyExpense
    }
  },
  onLoad() {
    // 注册导航栏按钮点击事件
    // uni.onNavigationBarButtonTap(this.onNavigationBarButtonTap);
  },
    
  // 处理导航栏按钮点击事件
  onNavigationBarButtonTap(e) {
    if (e.index === 0) { // 设置按钮
      this.goToSettings();
    }
  },
  methods: {
    fabClick() {
      console.log('FAB clicked');
      uni.navigateTo({
        url: '/pages/finance/create'
      })
    },
    trigger(e) {
      console.log(e);
    },
    addIncome() {
      uni.navigateTo({
        url: '/pages/finance/create?type=income'
      })
    },
    addExpense() {
      uni.navigateTo({
        url: '/pages/finance/create?type=expense'
      })
    },
    viewAllTransactions() {
      uni.navigateTo({
        url: '/pages/finance/transactions'
      })
    },
    
    addIncome() {
      uni.navigateTo({
        url: '/pages/finance/create?type=income'
      })
    },
    
    addExpense() {
      uni.navigateTo({
        url: '/pages/finance/create?type=expense'
      })
    },
    
    createTransaction() {
      uni.navigateTo({
        url: '/pages/finance/create'
      })
    },
    
    viewAllTransactions() {
      uni.showToast({
        title: '查看全部交易功能开发中',
        icon: 'none'
      })
    },
    
    goToSettings() {
      uni.navigateTo({
        url: '/pages/settings/index'
      })
    }
  }
}
</script>

<style scoped>
.finance-page {
  background-color: #F8FAFC;
  min-height: 100vh;
}

/* 自定义导航栏样式 */
.header-left {
  display: flex;
  align-items: center;
}

.page-title {
  font-size: 36rpx;
  font-weight: 600;
  color: #1F2937;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 24rpx;
}

/* 主要内容区域 */
.main-content {
  padding-top: 16px;
  background-color: #F8FAFC;
  min-height: calc(100vh - 16px);
}

/* 卡片标题样式 */
.card-title {
  display: flex;
  align-items: center;
  gap: 8px;
}

.title-text {
  font-size: 32rpx;
  font-weight: 600;
  color: #1F2937;
}

/* 财务概览统计 */
.summary-stats {
  display: flex;
  justify-content: space-between;
  gap: 24rpx;
}

.stat-item {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 12rpx;
  padding: 16rpx;
  background-color: #F8FAFC;
  border-radius: 12rpx;
}

.stat-icon {
  width: 40rpx;
  height: 40rpx;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.stat-item.income .stat-icon {
  background-color: #DCFCE7;
}

.stat-item.expense .stat-icon {
  background-color: #FEE2E2;
}

.stat-item.balance .stat-icon {
  background-color: #DBEAFE;
}

.stat-content {
  flex: 1;
  min-width: 0;
}

.stat-value {
  font-size: 28rpx;
  font-weight: 600;
  color: #1F2937;
  line-height: 1.2;
  margin-bottom: 4rpx;
}

.stat-label {
  font-size: 24rpx;
  color: #6B7280;
}

/* 快速操作 */
.quick-actions {
  display: flex;
  gap: 0;
  margin: 0 16px;
}

.action-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
}

.action-text {
  font-size: 28rpx;
  font-weight: 500;
  color: #374151;
}

/* 交易列表区域 */
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.section-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.section-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #1F2937;
}

.view-all {
  font-size: 28rpx;
  color: #3B82F6;
  font-weight: 500;
}

/* 交易列表 */
.transaction-list {
  display: flex;
  flex-direction: column;
}

.transaction-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16rpx 0;
  border-bottom: 1px solid #F1F5F9;
}

.transaction-item.last-item {
  border-bottom: none;
}

.transaction-left {
  display: flex;
  align-items: center;
  gap: 12rpx;
  flex: 1;
  min-width: 0;
}

.transaction-icon {
  width: 40rpx;
  height: 40rpx;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.transaction-icon.income {
  background-color: #DCFCE7;
}

.transaction-icon.expense {
  background-color: #FEE2E2;
}

.transaction-info {
  flex: 1;
  min-width: 0;
}

.transaction-category {
  font-size: 30rpx;
  font-weight: 500;
  color: #1F2937;
  line-height: 1.3;
  margin-bottom: 4rpx;
}

.transaction-description {
  font-size: 26rpx;
  color: #6B7280;
  line-height: 1.2;
  margin-bottom: 4rpx;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.transaction-date {
  font-size: 24rpx;
  color: #9CA3AF;
}

.transaction-amount {
  font-size: 30rpx;
  font-weight: 600;
  flex-shrink: 0;
}

.transaction-amount.income {
  color: #10B981;
}

.transaction-amount.expense {
  color: #EF4444;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60rpx 40rpx;
  text-align: center;
}

.empty-text {
  font-size: 32rpx;
  font-weight: 500;
  color: #6B7280;
  margin: 24rpx 0 12rpx 0;
}

.empty-subtitle {
  font-size: 28rpx;
  color: #9CA3AF;
  line-height: 1.5;
}

/* 响应式调整 */
@media (max-width: 750rpx) {
  .summary-stats {
    flex-direction: column;
    gap: 16rpx;
  }
  
  .stat-item {
    justify-content: flex-start;
  }
  
  .transaction-description {
    max-width: 200rpx;
  }
}

.summary-section {
  padding: 0 32rpx;
  margin-bottom: 48rpx;
}

.section-title {
  font-size: 36rpx;
  font-weight: 600;
  color: #1F2937;
  display: block;
  margin-bottom: 32rpx;
}

.summary-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24rpx;
  margin-bottom: 32rpx;
}

.summary-card {
  background-color: #FFFFFF;
  padding: 32rpx;
  border-radius: 16rpx;
  border: 1px solid #E5E7EB;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 12rpx;
  margin-bottom: 16rpx;
}

.card-icon {
  font-size: 32rpx;
}

.amount {
  font-size: 36rpx;
  font-weight: 700;
  display: block;
  margin-bottom: 8rpx;
}

.amount.positive {
  color: #10B981;
}

.amount.negative {
  color: #EF4444;
}

.change {
  font-size: 24rpx;
  font-weight: 500;
}

.change.positive {
  color: #10B981;
}

.change.negative {
  color: #EF4444;
}

.income-section {
  margin-bottom: 32rpx;
}

.subsection-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #1F2937;
  display: block;
  margin-bottom: 24rpx;
}

.income-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24rpx;
}

.income-card {
  background-color: #fff;
  border-radius: 12px;
  padding: 15px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: space-between;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  border: 1px solid #eee;
  position: relative;
}

.income-card.active {
  border-color: #42b983; /* Example active border color */
}

.income-card.passive {
  border-color: #f0ad4e; /* Example passive border color */
}

.income-icon {
  font-size: 24px;
  margin-bottom: 10px;
  position: absolute;
  top: 15px;
  left: 15px;
}

.income-card.active .income-icon {
  color: #42b983;
}

.income-card.passive .income-icon {
  color: #f0ad4e;
}

.income-label {
  font-size: 14px;
  color: #666;
  margin-top: 30px; /* Adjust to make space for icon */
}

.income-amount {
  font-size: 20px;
  font-weight: bold;
  color: #333;
}

.ratio-card {
  background-color: #FFFFFF;
  padding: 32rpx;
  border-radius: 16rpx;
  border: 1px solid #E5E7EB;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
}

.ratio-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16rpx;
}

.ratio-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #1F2937;
}

.ratio-percentage {
  font-size: 36rpx;
  font-weight: 700;
  color: #10B981;
}

.ratio-bar {
  height: 16rpx;
  background-color: #E5E7EB;
  border-radius: 8rpx;
  overflow: hidden;
  margin-bottom: 12rpx;
}

.ratio-fill {
  height: 100%;
  background-color: #10B981;
  border-radius: 8rpx;
  transition: width 0.3s ease;
}

.ratio-target {
  font-size: 24rpx;
  color: #6B7280;
}

.transactions-section {
  padding: 0 32rpx;
  margin-bottom: 48rpx;
}

.transaction-list {
  display: flex;
  flex-direction: column;
  gap: 24rpx;
}

.transaction-item {
  background-color: #FFFFFF;
  padding: 32rpx;
  border-radius: 16rpx;
  border: 1px solid #E5E7EB;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
}

.transaction-info {
  display: flex;
  align-items: center;
  gap: 24rpx;
  flex: 1;
}

.transaction-details {
  flex: 1;
}

.transaction-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #1F2937;
  display: block;
  margin-bottom: 8rpx;
}

.transaction-category {
  font-size: 24rpx;
  color: #6B7280;
}

.transaction-amount.positive {
  color: #10B981;
}

.transaction-amount.negative {
  color: #EF4444;
}

.action-button {
  background-color: #FFFFFF;
  padding: 32rpx;
  border-radius: 16rpx;
  border: 1px solid #E5E7EB;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
}

.action-icon {
  font-size: 48rpx;
}

.income-btn {
  border-color: #10B981;
  background-color: #ECFDF5;
}

.expense-btn {
  border-color: #EF4444;
  background-color: #FEF2F2;
}


</style>